package com.xfb.user.service;

import com.xfb.user.dao.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class UserService {

    @Resource
    UserMapper userMapper;

    public String selectUser(String email) {
        Map selectUser = userMapper.selectUser(email);
        if (selectUser == null) {
            return "false";
        }
        return selectUser.get("email").toString();
    }

    public Map selectUserToken(String token) {
        Map map = userMapper.selectUserToken(token);
        return map;
    }

    public int selectRegisterCode(String email, String code) {
        Map map = userMapper.selectRegisterCode(email, code);
        if (map == null) {
            return 3;
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date expiretime = new Date();
        try {
            expiretime = df.parse(map.get("expiretime").toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (new Date().after(expiretime)) {
            return 2;
        }
        return 0;
    }

    public int insertToken(String email, String token) {
        return userMapper.insertToken(email, token);
    }
}
